package com.example.pixelyear.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.pixelyear.R
import com.example.pixelyear.bean.ColorBean
import com.socks.library.KLog
import kotlinx.android.synthetic.main.item_landscape_color.view.*

class ColorLandscapeAdapter(val context: Context) : BaseAdapter<ColorBean>() {

    var lastChoose = 0
    var nowChoose = 0
    private var isFirst = true
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        return BaseViewHolder(LayoutInflater.from(context).inflate(R.layout.item_landscape_color, parent, false))
    }

    override fun onBindView(itemView: View, bean: ColorBean, position: Int) {
        KLog.e("yxs", "颜色值：" + bean.color)
        itemView.Item_Color.setColor(bean.color, bean.text)
        if (nowChoose == position) {
            itemView.Item_Color.choose()
        } else {
            itemView.Item_Color.notChoose()
        }

        if (isFirst) {
            onItemClick?.click(bean, position)
            isFirst = false
        }

        itemView.setOnClickListener {
            onItemClick?.click(bean, position)
            nowChoose = position
            notifyItemChanged(nowChoose)
            if (lastChoose != -1)
                notifyItemChanged(lastChoose)
            lastChoose = position
        }
    }

    fun updatePosition(index: Int, bean: ColorBean) {
        isFirst = false
        onItemClick?.click(bean, index)
        nowChoose = index
        notifyItemChanged(nowChoose)
        if (lastChoose != -1)
            notifyItemChanged(lastChoose)
        lastChoose = index
    }

}
